<?php

namespace SinoPHP\BbdBoss\Api;

use InvalidArgumentException;
use Upfor\UpforPHP\Library\Math;

/**
 * 获取平台域名与策略绑定关系
 *
 * 用于API系统获取平台域名与策略的绑定关系
 *
 */
class PlatDomainPolicy extends ApiAbstract {

    public function v_1_0_0() {
        $field = 'plat_domain.plat_domain_id,plat_domain.subzone,plat_domain.domain,plat_domain.server_group_ids,plat_domain_line.isp,plat_domain_line.province,plat_domain_line.plat_domain_line_id as view_id,plat_domain_line.policy_id scheduling_strategy_id,plat_domain_line.ttl';
        $list = model('PlatDomainLine')->getLineList($field);
        if (!$list) {
            throw new InvalidArgumentException('', 107001);
        }
        $lineNum = model("DomainDnspodLine")->count(['is_group'=>0,'status'=>1,'nav_group_id|!'=>0]);
        $data['list'] = array();
        foreach ($list as $value) {
        	if ( !isset($data['list'][$value['plat_domain_id']]) ) {
                $data['list'][$value['plat_domain_id']]['id']               = (int)$value['plat_domain_id'];
        		$data['list'][$value['plat_domain_id']]['join_domain_id']   = $value['subzone'].$value['domain'];
        		$data['list'][$value['plat_domain_id']]['server_group_ids'] = $value['server_group_ids'];
        		$data['list'][$value['plat_domain_id']]['views'][]          = array(
        			'isp'                    => $value['isp'],
        			'view_id'                => (int)$value['view_id'],
                    'province'               => $value['province'],
        			'scheduling_strategy_id' => (int)$value['scheduling_strategy_id'],
        			'ttl'                    => (int)$value['ttl'],
        			);
        	} else {
        		$data['list'][$value['plat_domain_id']]['views'][] = array(
        			'isp'                    => $value['isp'],
        			'view_id'                => (int)$value['view_id'],
                    'province'               => $value['province'],
        			'scheduling_strategy_id' => (int)$value['scheduling_strategy_id'],
        			'ttl'                    => (int)$value['ttl'],
        			);
        	}
        }
        foreach ($data['list'] as $l_key => $l_val) {
            $views_num = model('PlatDomainLine')->count(['plat_domain_id'=>$l_val['id']]);
            if ( $views_num != ($lineNum + 1) ) {
                unset($data['list'][$l_key]);
            }
        }
        $data['list'] = array_values($data['list']);

        $this->success($data['list']);
    }

}
